//
//  CarTableViewCell.m
//  易车界面
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CarTableViewCell.h"
#import "CarModel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"

@interface CarTableViewCell ()

@property (nonatomic, strong) UIImageView *imageViewLeft;
@property (nonatomic, strong) UIImageView *imageViewMiddle;
@property (nonatomic, strong) UIImageView *imageViewRight;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, assign) CGFloat cellHeight;

@end

@implementation CarTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews
{
    _imageViewLeft = [[UIImageView alloc]init];
    [self.contentView addSubview:_imageViewLeft];
    
    _imageViewMiddle = [[UIImageView alloc]init];
    [self.contentView addSubview:_imageViewMiddle];
    
    _imageViewRight = [[UIImageView alloc]init];
    [self.contentView addSubview:_imageViewRight];
    
    _titleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:_titleLabel];
}

- (void)prepareForReuse
{
    _imageViewRight.hidden = YES;
    _imageViewMiddle.hidden = YES;
    _titleLabel.hidden = YES;
    [super prepareForReuse];
}

- (void)setModel:(CarModel *)model
{
    _model = model;
    if ([model.picCover componentsSeparatedByString:@";"].count == 1) {
        _imageViewMiddle.hidden = YES;
        _imageViewRight.hidden = YES;
        _titleLabel.hidden = NO;
        [_imageViewLeft sd_setImageWithURL:[NSURL URLWithString:model.picCover] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        _titleLabel.text = model.title;
    } else {
        _imageViewMiddle.hidden = NO;
        _imageViewRight.hidden = NO;
        _titleLabel.hidden = YES;

        NSArray *imgArr = [model.picCover componentsSeparatedByString:@";"];
        [_imageViewLeft sd_setImageWithURL:[NSURL URLWithString:imgArr[0]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        [_imageViewMiddle sd_setImageWithURL:[NSURL URLWithString:imgArr[1]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        [_imageViewRight sd_setImageWithURL:[NSURL URLWithString:imgArr[2]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }
}



- (void)layoutSubviews
{
    CGFloat imgWidth = ([UIScreen mainScreen].bounds.size.width - 20)/3;
    CGFloat imgHeight = [UIImage imageNamed:@"placeholder"].size.height/[UIImage imageNamed:@"placeholder"].size.width *imgWidth;
    _cellHeight = 20 + imgHeight;
    [_imageViewLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(imgWidth, imgHeight));
        make.top.equalTo(self.contentView).with.offset(10);
        make.left.equalTo(self.contentView).with.offset(10);
    }];
    if (_imageViewRight.hidden == YES) {
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_imageViewLeft.mas_right).with.offset(10);
            make.right.equalTo(self.contentView);
            make.centerY.equalTo(_imageViewLeft);
        }];
        return;
    }
    [_imageViewMiddle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_imageViewLeft.mas_right);
        make.size.equalTo(_imageViewLeft);
        make.top.equalTo(self.contentView).with.offset(10);
    }];
    [_imageViewRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_imageViewMiddle.mas_right);
        make.size.equalTo(_imageViewMiddle);
        make.top.equalTo(self.contentView).with.offset(10);
    }];
}

//- (void)updateConstraints
//{
//    [self mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(_cellHeight);
//        make.width.equalTo(self.superview);
//    }];
////    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
////        make.edges.equalTo(self);
////    }];
//    [super updateConstraints];
//}


- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

@end
